package org.dna.metronomo.persistence.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.dna.metronomo.persistence.IUserDAO;
import org.dna.metronomo.persistence.PersistenceException;
import org.dna.metronomo.persistence.User;
import org.dna.metronomo.persistence.impl.annotations.SessionField;
import org.dna.metronomo.persistence.impl.annotations.SessionMethod;

public class IBatisUserDAO  implements IUserDAO {

    private final static Logger LOG = Logger.getLogger(IBatisUserDAO.class);

    @SessionField
    SqlSession m_session;
    
    @SessionMethod
    public User findUser(final String username, final String password) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("username", username);
        param.put("password", password);
        return (User) m_session.selectOne("UserMapper.selectByUserNamePwd", param);
    }

    @SessionMethod
    public void addNewUser(final User user) {
        m_session.insert("UserMapper.insert", user);
    }

    @SessionMethod
    public void update(final User user) {
        m_session.update("UserMapper.update", user);
    }

    @SessionMethod
    public List<User> users() {
        List<User> res = (List<User>) m_session.selectList("UserMapper.allUsers");
        LOG.debug("users returned " + res);
        return res;
    }

    @SessionMethod
    public void deleteByExample(final User... users) throws PersistenceException {
        for (User user : users) {
//            if (user.getJobs() == null || user.getJobs().isEmpty()) {
            m_session.delete("UserMapper.deleteByExample", user);
//            } else {
//                Map<String, Object> param = new HashMap<String, Object>();
//                param.put("user", user);
//                param.put("status", STATUS_DELETED);
//                m_session.update("UserMapper.updateStatusByExample", param);
//            }
        }
    }

    @SessionMethod
    public void deleteByID(final int... ids) throws PersistenceException {
        LOG.debug("deleting by IDs " + Arrays.toString(ids));
        for (int id : ids) {
            User user = (User) m_session.selectOne("UserMapper.queryByID", id);

            if (user == null) {
                continue;
            }

//            if (user.getJobs() == null || user.getJobs().isEmpty()) {
//                UserMapper mapper = m_session.getMapper(UserMapper.class);
//                mapper.deleteByID(id);
//            } else {
            Map params = new HashMap();
            params.put("id", id);
            params.put("status", Constants.STATUS_DELETED);
            m_session.update("UserMapper.updateStatusByID", params);
//            }
            }
    }

    @SessionMethod
    public User findByID(final int id) throws PersistenceException {
        LOG.debug("find by ID " + id);
//        Map<String, String> param = new HashMap<String, String>();
        return (User) m_session.selectOne("UserMapper.selectByID", id);
    }
}
